package com.manage.action;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.manage.po.User;
import com.manage.service.IUserService;
import com.manage.util.VerificationCodeUtil;

  
@Controller
@Results({ @Result(name = "input", location = "../login.jsp"),
	    @Result(name = "pwd_edit", location = "admin/pwd_modify.jsp"),
		@Result(name = "succ", location = "main/main.jsp") })
public class LoginAction extends BaseAction {

	private static final long serialVersionUID = 5033459742031477727L;

	@Autowired
	private IUserService userService;

	private String userid;
	private String password;
	private String checkcode;

	private String oldpassword;
	private String newpassword;
	private String confirmpassword;
	private String pwd;

	private ByteArrayInputStream imageStream;

	@Action("Login_Index")
	public String index() {
		return "input";
	}

	@Action(value = "SecurityCodeImage", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "image/jpeg", "inputName", "imageStream",
			"bufferSize", "2048" }) })
	public String securityCodeImage() {
		VerificationCodeUtil vcu = VerificationCodeUtil.Instance();
		imageStream = vcu.getImage();
		session.put("random", vcu.getVerificationCodeValue());
		return SUCCESS;
	}

	@Action("Login_Login")
	public String login() throws Exception {
		String yzm = this.checkcode.toLowerCase();// 将验证码字符串全部转换成小写
		String random = session.get("random").toString().toLowerCase();
		if (!yzm.equals(random)) {
			Map<String, String> jsonMap = new HashMap<String, String>();
			jsonMap.put("success", "false");
			jsonMap.put("msg", "验证码错误，请核实后重新输入!");
			return this.ajaxJsonMap(jsonMap);
		} else {
			User user = userService.getUserById(userid);
			if (user != null) {
				// String pwd=DESPlus.encrypt("0", password);
				
				String pwd = password;
				if (!user.getPassword().equals(pwd)) {
					Map<String, String> jsonMap = new HashMap<String, String>();
					jsonMap.put("success", "false");
					jsonMap.put("msg", "密码错误，请核实后重新输入!");
					return this.ajaxJsonMap(jsonMap);
				}
				// else if(user.getIsUse().equals("0"))
				// OutJson.outString("{success:false,msg:'该用户已停用!'}");
				// else if(!user.getType().equals("管理员"))
				// {
				// OutJson.outString("{success:false,msg:'非管理员不允许登录!'}");
				// }
				else {
					session.put("uid", user.getUserid());   //将用户的Id存入session中，以便于查找是否存在这个用户
					session.put("uname", user.getUsername());
					session.put("role", user.getRoleid()); //1 超级管理员  2 助管
					Map<String, String> jsonMap = new HashMap<String, String>();
					jsonMap.put("success", "true");
					jsonMap.put("msg", "登录成功!");
					return this.ajaxJsonMap(jsonMap);
				}
			} else {
				Map<String, String> jsonMap = new HashMap<String, String>();
				jsonMap.put("success", "false");
				jsonMap.put("msg", "登录失败，用户名或者密码不正确!");
				return this.ajaxJsonMap(jsonMap);
			}
		}
	}

	@Action("Login_LoginOut")
	public String loginout() throws Exception {
		session.remove("uid");
		session.remove("uname");
		return "input";
	}

	@Action("Look_PWD")
	public String retpwd(){
		return "pwd_edit";
	}
	
	
	@Action("Login_UpdatePwd")
	public String updatepwd() {
		
		String userid = (String) session.get("uid");
		
		if (!confirmpassword.equals(newpassword)) {
			return this.ajaxJsonWarningMessage("输入密码和确认密码不一致!");
		} else {
			User admin = userService.getUserById(userid);
			String pwd = admin.getPassword();
			try {
				//pwd = DESPlus.encrypt("0", mypwd);
				//oldpassword = DESPlus.encrypt("0", oldpassword);
			} catch (Exception e) {
				e.printStackTrace();
			}
			System.out.println(userid + "----*********----" + pwd + "---*********---" + oldpassword);
			if (!oldpassword.equals(pwd)) {
				return this.ajaxJsonWarningMessage("原密码输入错误!");
			} else {
				try {
					//newpassword = DESPlus.encrypt("0", newpassword);
				} catch (Exception e) {
					e.printStackTrace();
				}
				admin.setPassword(newpassword);
				userService.merge(admin);
				return this.ajaxJsonSuccessMessage("修改成功!", "1", "closeCurrent");
			}
		}
		
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getCheckcode() {
		return checkcode;
	}

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	public String getOldpassword() {
		return oldpassword;
	}

	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}

	public String getNewpassword() {
		return newpassword;
	}

	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}

	public String getConfirmpassword() {
		return confirmpassword;
	}

	public void setConfirmpassword(String confirmpassword) {
		this.confirmpassword = confirmpassword;
	}

	public ByteArrayInputStream getImageStream() {
		return imageStream;
	}

	public void setImageStream(ByteArrayInputStream imageStream) {
		this.imageStream = imageStream;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

}